import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;

public class Zony extends Actor
{


    //((it) getWorld()).ok(x,y);
    int counter=0,x=0,a=0,velY=0,image=0,animate=11,animator=0,push=0,health=10;
    int weapon=0,upgrade=0,move=0;
    boolean right=true,pressed=false,throwing=false,arm=false,shootable=true,disabled=false;
    Color alpha = new Color(0,0,0,0);
    String str;
    control contra;
    GreenfootImage buffer = new GreenfootImage(28,28);
    GreenfootImage[] z = {new GreenfootImage("Zony0.bmp"),
                          new GreenfootImage("Zony1.bmp"),
                          new GreenfootImage("Zony2.bmp"),
                          new GreenfootImage("Zony3.bmp"),
                          new GreenfootImage("Zony4.bmp"),
                          new GreenfootImage("Zony5.bmp"),
                          new GreenfootImage("Zony6.bmp"),
                          new GreenfootImage("Zony7.bmp"),
                          new GreenfootImage("Zony8.bmp"),
                          new GreenfootImage("Zony9.bmp"),
                          new GreenfootImage(28,28)};
                         
    
    public Zony(int up,int wep){
    
        setImage(buffer);
        upgrade=up;
        weapon=wep;
        for(image=0;image<10;image++){
            for(int x=0;x<84;x++){
                for(int y=0;y<28;y++){
                    if(z[image].getColorAt(x,y).equals(Color.white))
                    z[image].setColorAt(x,y,alpha);
                }
            }
        }   
    }
    protected void addedToWorld(World world)
    {
        z[10]=((it)getWorld()).getEnd();
        contra = (control) getWorld().getObjects(control.class).get(0);
    }   
    
    
    public void act() 
    {
        if(getY()==399 || Greenfoot.isKeyDown("r"))
            contra.done(true);
        if(getX()>=596 || Greenfoot.isKeyDown("1")){
            contra.done(false);
            return;
        }
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.isKeyDown("m") && mouse!=null && Greenfoot.mouseClicked(null)){
            setLocation(mouse.getX(),mouse.getY());
        }
   
        if(Greenfoot.isKeyDown("I"))
            health=-1;
        if(health==0){
            a++;
            buffer.clear(); 
            if(getY()<399)buffer.drawImage(z[10],0,0);
            setLocation(getX(),getY()+velY);
            if(a%4==2 && velY!=10)velY++; 
            return;
        }
        
        
        
        shootable=true;
        physics();  //physics simulation
        animate++;      //animation for attacks
        if(counter==12)counter=3;
        if(animate>10){
            if(counter==0)image=0; 
            if(counter==3)image=1;
            if(counter==6)image=2;
            if(counter==9)image=3;
            if((ok(getX(),getY()+18) || disabled) && velY>0)image=4;
            if((ok(getX(),getY()+18) || disabled) && velY<0)image=5;
        }
        
        if(Greenfoot.isKeyDown("d")){     //primary attack
            if(weapon==1 && upgrade>0)sword(1);
            if(weapon==2 && upgrade>2)gun(1);
        }else  pressed=false;
 
        if(Greenfoot.isKeyDown("a")){                       //secondary attack
            if(weapon==1 && upgrade>1)sword(2);
            if(weapon==2 && upgrade>3)gun(2);
        }

        
        if(animate==3)image=animator;       //boomerang imgage systems
        if(animate==7)image=0;     
        if(arm)image=8; 
        if(throwing)if(((it) getWorld()).isCaught()){
            throwing=false;
            arm=false;
        }
        
        enemy();        //check for enemies
        upgrade();        //check for upgrades
        draw();         //draw the figure
        if(health==0)velY=-8;
    }  
    
    
//***************************************************************************
//***************************************************************************      
    
    public void gun(int key){
       if(key==1 && !pressed && animate>10){
            image=6;
            animate=0;
            animator=7;
            pressed=true;  
            Greenfoot.playSound("shot.wav");
            ((it) getWorld()).addObject(new bullet(right),getX()+7,getY()-4+Greenfoot.getRandomNumber(3));
        }
        
        if(key==2 && animate>3 && shootable){
            image=6;
            animate=0;
            animator=7;
            pressed=true;  
            Greenfoot.playSound("shot.wav");
            ((it) getWorld()).addObject(new bullet(right),getX()+7,getY()-4+Greenfoot.getRandomNumber(3));
        }   
    }
    
//***************************************************************************
//***************************************************************************  
    
    public void sword(int key){
        if(key==1 && !pressed && animate>10 && !throwing){
            image=6;
            animate=0;
            animator=7;
            pressed=true;
            Greenfoot.playSound("swordWhip.wav");   
        }
        
        a=-1;
        if(right)a=1;
        
        if(key==2 && getX()>28 && getX()<572 && ok(getX()+26*a,getY()) && !throwing && ok(getX()+8*a,getY()) && ok(getX()+14*a,getY())){
        throwing=true;
            if(!ok(getX(),getY()+14))arm=true;
            ((it) getWorld()).thrown();
            if(right)
                 ((it) getWorld()).addObject(new sword(true),getX()+13,getY());
            else
                 ((it) getWorld()).addObject(new sword(false),getX()-13,getY());
         }
     }
    
//***************************************************************************
//***************************************************************************  
    
    public void physics(){

        if(str!=null && str.equals("s")){
            if(weapon==2)weapon=3;
            if(weapon==1 && upgrade>2)weapon=4;  
            if(weapon>2)weapon=weapon-2;
        }
        move=0;
        if(getY()>13 && ((it)getWorld()).kill(getX(),getY()-13) && !((it)getWorld()).kill(getX(),getY()-6) && ((it)getWorld()).ok(getX(),getY()-6))
            health=0;
        
        if(!Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("right") && push==0)
             counter=0;
        else
        {
            counter++;
            if(push>0)push--;
            if(push<0)push++;
            if((push<0 || Greenfoot.isKeyDown("left"))){
                move=-2;
                right=false;
                arm=false;
                shootable=false;
                if(push<0)
                    right=true;
                setLocation(getX()-2,getY());
            }
            if((push>0 || Greenfoot.isKeyDown("right"))){
                move=2;
                right=true;              
                arm=false;
                shootable=false;
                if(push>0)
                    right=false;
                setLocation(getX()+2,getY());
            }
        }
        
        if(move!=0){
            if(!ok(getX(),getY()+11) || object(getX()+move,getY()-2))
                setLocation(getX()-move,getY());
        }else{
            if(object(getX()+4,getY()-2) && ok(getX()-2,getY()))
                setLocation(getX()-2,getY());
            if(object(getX()-4,getY()-2) && ok(getX()+2,getY()))
                setLocation(getX()+2,getY());
        }
        
        if(((ok(getX(),getY()+13) && !object(getX(),getY()+1)) || disabled || velY>4) && velY!=-13)
            velY--;
        else
            if(velY!=-13)velY=0;
        
        if(velY<2 && Greenfoot.isKeyDown("up") && !disabled && (!ok(getX(),getY()+16) || object(getX(),getY()+1))){
            velY=13;
            arm=false;
        }
      
        setLocation(getX(),getY()-(int)(velY/2));

        if(ok(getX(),getY()+13) && !ok(getX(),getY()+7))
            disabled=true;
        if(ok(getX(),getY()+13) && ok(getX(),getY()))
            disabled=false;
        
        if(velY<2 && !disabled){
            while(!ok(getX(),getY()+13) || object(getX(),getY())){
                velY=0;
                setLocation(getX(),getY()-1);  
            }
        }
        
    }
    
//***************************************************************************
//***************************************************************************       
    public void enemy(){
        Enemy it = (Enemy) getOneIntersectingObject(Enemy.class);
        if(it!=null && push==0 && it.isDeadly() && !disabled){
            if(Math.abs(it.getX()-getX())<13){
                health--;
                if(it.getX() > getX())push=-10;
                else  push=10;
                image=9;
                if(health==0)velY=-8;
            }
            if(animate<10 && ((it.getX()>getX() && right) || (it.getX()<getX() && !right)))
                it.hurt(1);
        }
         
    }
    
//***************************************************************************
//***************************************************************************  
    public void draw(){        
        if(weapon==1){
            buffer.clear();
            buffer.drawImage(z[image],0,0);    
        }
        if(weapon==0 || (throwing && !arm)){
            buffer.clear();
            buffer.drawImage(z[image],-28,0);
        }
        if(weapon==2){
            buffer.clear();
            buffer.drawImage(z[image],-56,0); 
        }
        if(push%2 != 0 )buffer.clear();
        if(!right)
            buffer.mirrorHorizontally();
    }
//***************************************************************************
//***************************************************************************      
    public void upgrade(){
        upgrade it = (upgrade) getOneIntersectingObject(upgrade.class);
        if(it!=null){
            upgrade = it.check();
            if(upgrade==1 || upgrade==2)weapon=1;
            if(upgrade==3 || upgrade==4)weapon=2;
        }    
    }
    
//***************************************************************************
//***************************************************************************  
    
    public boolean ok(int a,int b){
        if(((it)getWorld()).kill(a,b)){
            health=0;
        } 
        return ((it)getWorld()).ok(a,b);
    }
    
//***************************************************************************
//*************************************************************************** 
    public int getWeapon(){
        return weapon;
    }
//***************************************************************************
//*************************************************************************** 
    public boolean object(int x, int y){
        int xTemp=getX(),yTemp=getY();
        setLocation(x,y);
        List boxes = getIntersectingObjects(object.class);
        boolean intersect = false;
        for(int f=0;f<boxes.size();f++){
            object box = (object)boxes.get(f);
            intersect = intersect || (Math.abs(box.getX()-x)<=(box.getImage().getWidth()/2) && box.stand());
        }
        setLocation(xTemp,yTemp);
        return intersect;
    }
//***************************************************************************
//***************************************************************************      
   
}
